package cn.com.yto56.coresystem.business.msg.soa.soa.impl;

import cn.com.yto56.coresystem.business.msg.soa.biz.IGroupInfoBiz;
import cn.com.yto56.coresystem.business.msg.soa.biz.IMsgListBiz;
import cn.com.yto56.coresystem.business.msg.soa.biz.IMsgTempletBiz;
import cn.com.yto56.coresystem.business.msg.soa.biz.ISysInfoBiz;
import cn.com.yto56.coresystem.business.msg.soa.biz.IUserGroupInfoBiz;
import cn.com.yto56.coresystem.business.msg.soa.biz.IUserInfoBiz;
import cn.com.yto56.coresystem.business.msg.soa.controller.YtoMsgServiceController;
import cn.com.yto56.coresystem.business.msg.soa.soa.IYtoMsgSoa;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.domain.AppResult;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2017-7-20    0020.
 */
public class YtoMsgSoaImpl implements IYtoMsgSoa {
    private final Logger logger = LoggerFactory.getLogger(YtoMsgServiceController.class);
    @Autowired
    private ISysInfoBiz sysInfoBizImpl;
    @Autowired
    private IUserInfoBiz userInfoBizImpl;
    @Autowired
    private IGroupInfoBiz groupInfoBizImpl;
    @Autowired
    private IUserGroupInfoBiz userGroupInfoBizImpl;
    @Autowired
    private IMsgTempletBiz msgTempletBizImpl;
    @Autowired
    private IMsgListBiz msgListBizImpl;
    @Override
    public String getAppId(Map params) {
        AppResult appResult = new AppResult();
        boolean resultFlag = Boolean.FALSE;
        String resultMessage = "";
        Map datas = null;
        Map paramDatas = (Map)params.get("datas");
        if(paramDatas == null){
            resultMessage = "请检查json格式是否有误";
        } else {
            String systemName = (String) paramDatas.get("yto_msg_systemName");
            String systemInfo = (String) paramDatas.get("yto_msg_systemInfo");
            String appId = sysInfoBizImpl.getAppId(systemName, systemInfo);
            if (StringUtils.isNotBlank(appId)) {
                datas = new HashMap();
                datas.put("yto_msg_appId", appId);
                resultFlag = true;
                resultMessage = "执行成功";
            } else {
                resultFlag = false;
                resultMessage = "该系统名已经注册或注册失败，请换一个系统名重试";
            }
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String registerUser(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String sys_userId = (String) resultParam.get("yto_msg_sys_userId");
            String sys_userName = (String) resultParam.get("yto_msg_sys_userName");
            String sys_remark = (String) resultParam.get("yto_msg_sys_remark");
            appResult = userInfoBizImpl.registerUserInfo(sysInfo_Id, sys_userId, sys_userName, sys_remark);
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String deleteUser(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String userId = (String) resultParam.get("yto_msg_userId");
            String status = (String) resultParam.get("yto_msg_status");
            String sys_remark = (String) resultParam.get("yto_msg_sys_remark");
            appResult = userInfoBizImpl.deleteUserInfo(sysInfo_Id, userId, status, sys_remark);
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String checkUser(Map params) {
        AppResult appResult = new AppResult();
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String userId = (String) resultParam.get("yto_msg_userId");
            appResult = userInfoBizImpl.checkUser(sysInfo_Id, userId);
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String addUserGroup(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String sys_userGroupId = (String) resultParam.get("yto_msg_sys_userGroupId");
            String sys_userGroupName = (String) resultParam.get("yto_msg_sys_userGroupName");
            String sys_remark = (String) resultParam.get("yto_msg_sys_remark");
            appResult = groupInfoBizImpl.addUserGroup(sysInfo_Id, sys_userGroupId, sys_userGroupName, sys_remark);
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String deleteUserGroup(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String userGroupId = (String) resultParam.get("yto_msg_userGroupId");
            String status = (String) resultParam.get("yto_msg_status");
            String sys_remark = (String) resultParam.get("yto_msg_sys_remark");
            appResult = groupInfoBizImpl.deleteUserGroup(sysInfo_Id, userGroupId, status, sys_remark);
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String modifyUserToGroup(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String userGroupId = (String) resultParam.get("yto_msg_userGroupId");
            String userId = (String) resultParam.get("yto_msg_userId");
            String optype = (String) resultParam.get("yto_msg_optype");
            if (StringUtils.isBlank(optype)) {
                optype = "1";
            }
            if (optype.equals("1")) {
                appResult = userGroupInfoBizImpl.addUserToGroup(sysInfo_Id, userGroupId, userId);
            } else {
                appResult = userGroupInfoBizImpl.deleteUserFromGroup(sysInfo_Id, userGroupId, userId);
            }
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String addMSGTemplet(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String templetInfo = (String) resultParam.get("yto_msg_templetInfo");
            String sys_remark = (String) resultParam.get("yto_msg_sys_remark");
            appResult = msgTempletBizImpl.addMsgTemplet(sysInfo_Id, templetInfo, sys_remark);
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String modifyMSGTemplet(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String msgTempletId = (String) resultParam.get("yto_msg_msgTempletId");
            String templetInfo = (String) resultParam.get("yto_msg_templetInfo");
            String optype = (String) resultParam.get("yto_msg_optype");
            String sys_remark = (String) resultParam.get("yto_msg_sys_remark");
            if (StringUtils.isBlank(optype)) {
                optype = "0";
            }
            if (optype.equals("0")) {
                appResult = msgTempletBizImpl.updateMsgTemplet(sysInfo_Id, msgTempletId, templetInfo, sys_remark);
            } else if (optype.equals("1")) {
                appResult = msgTempletBizImpl.deleteMsgTemplet(sysInfo_Id, msgTempletId, sys_remark);
            }
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String sendMSGByTemplet(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String receiverId = (String) resultParam.get("yto_msg_receiverId");
            String receiverType = (String) resultParam.get("yto_msg_receiverType");
            String msgTempletId = (String) resultParam.get("yto_msg_msgTempletId");
            Map msgTempletParam = (Map) resultParam.get("yto_msg_msgTempletParam");
            String sendTime = (String) resultParam.get("yto_msg_sendTime");
            appResult = msgListBizImpl.addMsgListForParam(sysInfo_Id, receiverId, receiverType, msgTempletId, msgTempletParam, sendTime);
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String getCurrentMSG(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String userId = (String) resultParam.get("yto_msg_userId");
            String fixed_sendTime = (String) resultParam.get("yto_msg_fixed_sendTime");
            appResult = msgListBizImpl.getMsgList(sysInfo_Id, userId, "0", fixed_sendTime, "");
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String getReSendMSG(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            String userId = (String) resultParam.get("yto_msg_userId");
            String sendTime = (String) resultParam.get("yto_msg_sendTime");
            appResult = msgListBizImpl.getMsgList(sysInfo_Id, userId, "1", "", sendTime);
        }
        return JSONObject.toJSONString(appResult);
    }

    @Override
    public String updateMsgListState(Map params) {
        AppResult appResult = new AppResult();
        //获取消息中心对应的系统Id
        appResult = getParamsMap(params);
        if (appResult.getResultFlag()) {
            Map resultParam = (Map) appResult.getDatas();
            String sysInfo_Id = (String) resultParam.get("yto_msg_sysInfo_Id");
            List<Map> yto_msg_msgLists = (List<Map>) resultParam.get("yto_msg_msgList");
            if (yto_msg_msgLists != null && yto_msg_msgLists.size() > 0) {
                for (int i = 0; i < yto_msg_msgLists.size(); i++) {
                    yto_msg_msgLists.get(i).put("yto_msg_sendState", "2");
                }
            }
            appResult = msgListBizImpl.updateMsgListState(yto_msg_msgLists);
        }
        return JSONObject.toJSONString(appResult);
    }

    public AppResult getParamsMap(Map params) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        String sysInfo_Id = "";
        String appId = (String) params.get("yto_msg_appId");
        if (StringUtils.isBlank(appId)) {
            resultMessage = "参数[yto_msg_appId]不能为空";
        } else {
            sysInfo_Id = sysInfoBizImpl.getSysInfoId(appId);
            if (StringUtils.isBlank(sysInfo_Id)) {
                resultMessage = "参数[yto_msg_appId]不合法";
            } else {
                Map paramDatas = (Map) params.get("datas");
                if (paramDatas == null) {
                    resultMessage = "请检查json格式是否有误";
                } else {
                    resultFlag = true;
                    paramDatas.put("yto_msg_sysInfo_Id", sysInfo_Id);
                    datas = paramDatas;
                }
            }
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }
}
